<?php

require_once '../application/controllers/Super_Controller.php';

class PagesController extends Super_Controller {

    public function indexAction() {
        $PGM = new Application_Model_Pages();
        $page = $PGM->getPage($this->getRequest()->getParam('page'));
        if (!empty($page['id'])) { // html page
            // add view page
            $this->view->viewPage = $page['view'];
            $PGM->incViewPage($page['id'], ++$this->view->viewPage);

            // get count comments
            $CMPM = new Application_Model_Comments();
            $this->view->comments = $CMPM->getComments($page['id']);
            $this->view->cntComments = count($this->view->comments);
            // write qs page
            if (!empty($_SERVER["HTTP_REFERER"])) {
                $PGM->writePageQs($_SERVER["HTTP_REFERER"], $page['id']);
                // add statistic of page
                $SPM = new Application_Model_Stat();
                $SPM->addStatPage($page['id'], $page['title'], $_SERVER, $this->getRealIp());
            }

            // get qs page
            $this->view->qs = $PGM->getPageQsArray($page['qs'], 30);

            // get content
            $this->view->content = $page['content'];
            $SSTM = new Application_Model_ShortStories();
            $iShortStoryCatID = $SSTM->getShortStoriesCategoriesIDByUrl($page['url']);
            if(!empty($iShortStoryCatID)) { // это short-story
                $this->view->aShotrStoriesInfo = $PGM->select('short_stories', '*', '`active`=1 AND `type`=' . $iShortStoryCatID, '`public_date` DESC, `id` DESC');
                $this->view->aShotrStoriesCat = $SSTM->getShortStoriesCat();
            }
            $this->view->is_comment = $page['is_comment'];
            $this->view->p_title = 'Developer blog :: ' . $page['page_title'];
            $this->view->p_key = $page['metakeywords'];
            $this->view->p_desc = $page['description'];
            $this->view->page_url = $page['url'];
            $this->view->page_id = $page['id'];
            $this->view->alike = $page['alike'];
        } else {
            throw new Zend_Controller_Action_Exception('This page does not exist 404', 404);
        }
    }

}

/*
 * Рассмотрим пример создания маршрутов для ссылок форматов указанных выше. Для начала нужно сформировать маршруты, для этого напишем метод в файле bootstrap.php:
  http://habrahabr.ru/post/121222/
 * public function _initRoute(){
  // Получаем маршрут, по-умолчанию
  $router = Zend_Controller_Front::getInstance()->getRouter();
  // создаем пользовательские маршруты
  // маршрут для статических страниц
  $route_static = new Zend_Controller_Router_Route(
  '/:page',
  array(
  'controller' => 'static',
  'action' => 'index',
  'page' => 'default-page-name'
  ),
  array(
  'page' => '[\w\-]+'
  )
  );
  $router->addRoute('static', $route_static);
  // маршрут для товаров
  $route_goods = new Zend_Controller_Router_Route(
  '/catalog/:category/:good/*',
  array(
  'controller' => 'goods',
  'action' => 'show'
  ),
  array(
  'category' => '[\w\-]+',
  'good' => '[\w\-]+'
  )
  );
  $router->addRoute('goods', $route_goods);
  }
 */

